package ch.mustaffah.aoide.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.mustaffah.aoide.player.PerpetuumPlayer;

public class MainApplicationFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel;
	private AoideKeyBord aoideKeyBord0;
	private JPanel applicationPanel;
	private JPanel commandPanel;
	private JButton perpetuumButton;
	private PerpetuumPlayer perpetuumPlayer;
	private JComboBox velocitySelectionComboBox;
	private JLabel phraseLenghtLabel;
	private JSlider phraseLengthSlider;
	private JCheckBox randomRhythmCheckBox;
	private AoideConfigurationPanel aoideConfigurationPanel;

	private AoideConfigurationPanel getConfigurationPanel() {
		if(this.aoideConfigurationPanel==null){
			this.aoideConfigurationPanel=new AoideConfigurationPanel();
		}
		return this.aoideConfigurationPanel;
	}

	public JButton getPerpetuumButton() {
		if (this.perpetuumButton == null) {
			this.perpetuumButton = new JButton("play");
			this.perpetuumButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					startPerpetuum();
				}
			});
		}

		return perpetuumButton;
	}
	
	public JComboBox getVelocitySelection() {
		if (this.velocitySelectionComboBox == null) {
			this.velocitySelectionComboBox = new JComboBox(VelocityMode.values());
			this.velocitySelectionComboBox.setSelectedItem(VelocityMode.Largo);
		}
		return this.velocitySelectionComboBox;
	}

	public MainApplicationFrame() {
		initializeView();
		createGui();
		pack();
		setVisible(true);
	}

	private void createGui() {
		setContentPane(getMainPanel());
	}

	private Container getMainPanel() {

		if (this.mainPanel == null) {
			this.mainPanel = new JPanel();
			this.mainPanel.add(getApplicationPanel());
		}
		return this.mainPanel;
	}
	

	private Component getApplicationPanel() {
		if (this.applicationPanel == null) {
			this.applicationPanel = new JPanel();
			this.applicationPanel.setLayout(new BoxLayout(
					this.applicationPanel, BoxLayout.Y_AXIS));
			this.applicationPanel.add(getAoideKeyBord0());
			this.applicationPanel.add(getCommandPanel());
		}
		return this.applicationPanel;
	}

	private Component getCommandPanel() {
		if (this.commandPanel == null) {
			this.commandPanel = new JPanel();
			this.commandPanel.setLayout(new BoxLayout(this.commandPanel, BoxLayout.Y_AXIS));
			this.commandPanel.add(getConfigurationPanel());
			JPanel sliderWrapper = new JPanel();
			sliderWrapper.setLayout(new BoxLayout(sliderWrapper, BoxLayout.X_AXIS));
			sliderWrapper.add(getPhraseLengthSlider());
			sliderWrapper.add(getPhraseLengthLabel());
			this.commandPanel.add(sliderWrapper);
			sliderWrapper.setVisible(false);
			JPanel lastWrapper = new JPanel();
			lastWrapper.setLayout(new BoxLayout(lastWrapper,BoxLayout.X_AXIS));
			lastWrapper.add(getRandomRhythmCheckBox());
			getRandomRhythmCheckBox().setVisible(false);
			lastWrapper.add(getVelocitySelection());
			getVelocitySelection().setVisible(false);
			lastWrapper.add(getPerpetuumButton());
			this.commandPanel.add(lastWrapper);
		}
		return this.commandPanel;
	}
	
	public JCheckBox getRandomRhythmCheckBox() {
		if (this.randomRhythmCheckBox == null) {
			this.randomRhythmCheckBox = new JCheckBox();
			this.randomRhythmCheckBox.setSelected(false);
			this.randomRhythmCheckBox.setText("Random Rhythm");
		}
		return this.randomRhythmCheckBox;
	}

	public JLabel getPhraseLengthLabel() {

		if (this.phraseLenghtLabel == null) {
			this.phraseLenghtLabel = new JLabel("128");
		}
		return this.phraseLenghtLabel;
	}

	public JSlider getPhraseLengthSlider() {
		if (this.phraseLengthSlider == null) {
			this.phraseLengthSlider = new JSlider(JSlider.HORIZONTAL, 1, 256, 128);
			this.phraseLengthSlider.setPreferredSize(new Dimension(100, 50));
			this.phraseLengthSlider.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent arg0) {
					getPhraseLengthLabel().setText(
							"" + phraseLengthSlider.getValue());
				}
			});
		}
		return this.phraseLengthSlider;
	}
	
	public AoideKeyBord getAoideKeyBord0() {
		if (this.aoideKeyBord0 == null) {
			this.aoideKeyBord0 = new AoideKeyBord(getConfigurationPanel());
		}
		return this.aoideKeyBord0;
	}

	
	private void initializeView() {		
		setTitle("Aoide, (c) 2012 by martin duerrenberger.");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new MainApplicationFrame();
	}

	private void startPerpetuum() {
		if (this.perpetuumPlayer == null) {
			this.perpetuumPlayer = new PerpetuumPlayer(this);
			perpetuumPlayer.setDaemon(true);
			perpetuumPlayer.start();
		}else{
			this.perpetuumPlayer.stop();
			this.perpetuumPlayer=null;
		}
	}
}
